package com.maimao.spider.processor;

import com.maimao.spider.controller.SpiderController;
import com.maimao.spider.model.Category;
import com.maimao.spider.regex.RegexUtils;
import com.maimao.spider.task.CategoryTask;
import com.maimao.spider.utils.PinYinUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Component;
import sun.security.provider.ConfigFile;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author MaoLin Wang
 * @date 2020/11/2419:45
 */
@Component
@Slf4j
public class CategoryProcessor implements PageProcessor {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

//    private static AtomicInteger count = new AtomicInteger(1);


    @Override
    public void process(Page page) {
        int index = SpiderController.count.getAndIncrement();
        if (index > 18) {
            SpiderController.count = new AtomicInteger(1);
            return;
        }

        StringBuilder sb;
        if (index<18){
            page.addTargetRequest(CategoryTask.all.get(index-1));
        }
        String cate1List = page.getHtml().xpath("//*[@id=\"J_cate\"]/ul/li[" + index + "]").get();


        //一级分类
        Category cate1 = new Category();
        Matcher matcher = RegexUtils.TAG_A.matcher(cate1List);
        sb = new StringBuilder();
        while (matcher.find()) {
            String cate1Name = matcher.group(2);
            if (StringUtils.isNotEmpty(cate1Name)) {
                sb.append(cate1Name).append("/");
            }


        }
        log.info("一级分类:{}", sb.toString());
        cate1.setName(sb.toString().substring(0, sb.toString().length() - 1));
        cate1.setCreateTime(LocalDateTime.now());
        cate1.setLevel(1);
        cate1.setPid(0);
        cate1.setStatus(1);
        cate1.setMobileName(Arrays.asList(cate1.getName().split("/")).get(0));
        cate1.setUpdateTime(cate1.getCreateTime());
        cate1.setPinyin(PinYinUtils.getPingYin2Json(cate1.getName(),"/"));
        cate1.setHead(PinYinUtils.getPingYinHead2Json(cate1.getName(),"/"));
        cate1.setOrder(index);

        List<String> cate2List = page.getHtml().xpath("//div[@id=\"cate_item" + (index) + "\"]/div[1]/div[2]//dt").all();
        List<String> cate3List = page.getHtml().xpath("//div[@id=\"cate_item" + (index) + "\"]//dd").all();

        page.putField("cate1", cate1);
        fetchCate2(cate2List, page, cate3List);
        fetchCate3(cate2List.size(), page, cate3List);
    }

    private void fetchCate2(List<String> cate2List, Page page, List<String> cate3List) {
        List<Category> categoryList = new ArrayList<>();
        for (int i = 0; i < cate2List.size(); i++) {
            Matcher cate2Matcher = RegexUtils.TAG_A.matcher(cate2List.get(i));
            if (cate2Matcher.find()) {
                //二级分类
                Category cate2 = new Category();
                String tagA = cate2Matcher.group(0);
                if (StringUtils.isNotEmpty(tagA)) {
                    String cate2Name = getCateName(tagA);
                    log.info("二级分类：{}", cate2Name);
                    cate2.setName(cate2Name);
                    cate2.setCreateTime(LocalDateTime.now());
                    cate2.setLevel(2);
                    cate2.setStatus(1);
                    cate2.setMobileName(Arrays.asList(cate2.getName().split("/")).get(0));
                    cate2.setUpdateTime(cate2.getCreateTime());
                    cate2.setPinyin(PinYinUtils.getPingYin2Json(cate2.getName(),"/"));
                    cate2.setHead(PinYinUtils.getPingYinHead2Json(cate2.getName(),"/"));
                    cate2.setOrder(i);

                    categoryList.add(cate2);

                }
            }
        }
        page.putField("cate2", categoryList);


    }

    private void fetchCate3(Integer cate2Size, Page page, List<String> cate3List) {
        //key 二级分类下标 value：对应的三级分类
        Map<Integer,List<Category>> res=new HashMap<>(cate2Size);
        for (int i = 0; i < cate2Size; i++) {
            Matcher cate3Matcher = RegexUtils.TAG_A.matcher(cate3List.get(i));
            List<Category> categoryList = new ArrayList<>();
            int count=0;
            while (cate3Matcher.find()) {
                //三级分类
                Category cate3 = new Category();
                String cate3Name = cate3Matcher.group(2);
                if (StringUtils.isNotEmpty(cate3Name)) {
                    log.info("三级分类：{}", cate3Name);
                    cate3.setName(cate3Name);
                    cate3.setCreateTime(LocalDateTime.now());
                    cate3.setLevel(3);
                    cate3.setStatus(1);
                    cate3.setMobileName(Arrays.asList(cate3.getName().split("/")).get(0));
                    cate3.setUpdateTime(cate3.getCreateTime());
                    cate3.setPinyin(PinYinUtils.getPingYin2Json(cate3.getName(),"/"));
                    cate3.setHead(PinYinUtils.getPingYinHead2Json(cate3.getName(),"/"));
                    cate3.setOrder(count++);


                    categoryList.add(cate3);
                }
            }
            res.put(i,categoryList);
        }
//        log.info("============================================");
        page.putField("cate3", res);

    }

    private String getCateName(String tagA) {
        int from = tagA.indexOf(">");
        return StringUtils.substring(tagA, from + 1, tagA.indexOf("<", from));
    }

    @Override
    public Site getSite() {
        return Site.me().setSleepTime(100).setRetryTimes(3);
    }

}
